﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using LibraryO2S;
using BusinessObjects;

public partial class controls_admin_ViewCategory : Pages_Base
{
    private static bool isNew;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (message.InnerHtml != "")
            message.InnerHtml = "";
        if (!IsPostBack)
        {
            isNew = true;
            getCategories();
            //lbCategory_SelectedIndexChanged(null, null);
        }
    }
    protected void lbCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            base.OpenConnection();
            if (!lbCategory.SelectedValue.Equals("0"))
            {
                int categoryID = Convert.ToInt32(lbCategory.SelectedValue);
                DataTable objDT = BO_HelpCategories.Search(categoryID);
                txtCategoryName.Text = objDT.Rows[0]["CATEGORY_NAME"].ToString();
                txtDescription.Text = objDT.Rows[0]["DESCRIPTION"].ToString();
                txtSortDisplay.Text = objDT.Rows[0]["SORT_DISPLAY"].ToString();
                isNew = false;
                imgIcon.Src = "../upload/help/icons/" + objDT.Rows[0]["THUMB_ICON"].ToString();
            }
        }
        catch (Exception ex) { throw ex; }
        finally { base.CloseConnection(); }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        txtCategoryName.Text = "";
        txtDescription.Text = "";
        txtSortDisplay.Text = "1";
        isNew = true;
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            base.OpenConnection();
            string categoryName = txtCategoryName.Text.Trim();
            string description = txtDescription.Text.Trim();
            int sort_display = Convert.ToInt32(txtSortDisplay.Text);
            string icon = string.Empty;
            if (fuIcon.HasFile)
            {
                icon = fuIcon.PostedFile.FileName;
                fuIcon.PostedFile.SaveAs(Server.MapPath("../upload/help/icons/" + icon));
            }
            DataTable admin = (DataTable)Session["Admin"];
            if (!isNew)
            {
                if (lbCategory.SelectedIndex == 0)
                    return;
                int categoryID = Convert.ToInt32(lbCategory.SelectedValue);
                DataTable objDT = BO_HelpCategories.Search(categoryID);
                if (!fuIcon.HasFile)
                    icon = objDT.Rows[0]["THUMB_ICON"].ToString();
                int parent = Convert.ToInt32(objDT.Rows[0]["PARENT_ID"]);
                DateTime createDate = Convert.ToDateTime(objDT.Rows[0]["CREATED_DATE"]);
                string createBy = objDT.Rows[0]["CREATED_BY"].ToString();
                BO_HelpCategories.Update(categoryID,
                    parent, categoryName, icon, description, sort_display, Convert.ToInt32(objDT.Rows[0]["ALTER_SORT"]),
                    createDate, createBy,
                    DateTime.Now, admin.Rows[0]["USER_NAME"].ToString(), true);
                message.InnerHtml = O2S_Message.Success("Thông Báo", "Sửa thành công !");
            }
            else
            {
                if (lbCategory.SelectedItem == null)
                    return;
                int parent = Convert.ToInt32(lbCategory.SelectedValue);
                BO_HelpCategories.Insert(
                    parent, categoryName, icon, description, sort_display, 1,
                    DateTime.Now, admin.Rows[0]["USER_NAME"].ToString(),
                    DateTime.Now, "", true);
                message.InnerHtml = O2S_Message.Success("Thông Báo", "Thêm thành công !");
            }
            btnAdd_Click(null, null);
            getCategories();
            lbCategory.SelectedIndex = 0;
        }
        catch (Exception ex)
        {
            message.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi <br/>" + ex.Message);
        }
        finally { base.CloseConnection(); }
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        try
        {
            base.OpenConnection();
            if (!lbCategory.SelectedValue.Equals("-1") && lbCategory.SelectedValue != null)
            {
                int categoryID = Convert.ToInt32(lbCategory.SelectedValue);
                if (BO_HelpCategories.Delete(categoryID))
                    message.InnerHtml = O2S_Message.Success("Thông Báo", "Xóa thành công !");
                else
                    message.InnerHtml = O2S_Message.Error("Lỗi", "Chưa thực hiện được !");
            }
            else
                message.InnerHtml = O2S_Message.Error("Yêu Cầu", "Chọn danh mục muốn xóa !");
            btnAdd_Click(null, null);
            getCategories();
        }
        catch { }
        finally { base.CloseConnection(); }
    }
    //protected void btnUp_Click(object sender, EventArgs e)
    //{
    //    try
    //    {
    //        int upCate = -1;
    //        int currentCate = Convert.ToInt32(lbCategory.SelectedValue);
    //        if (lbCategory.SelectedIndex > 1)
    //        {
    //            bool flag = false;
    //            for (int i = lbCategory.SelectedIndex - 1; i >= 0; i--)
    //            {
    //                upCate = Convert.ToInt32(lbCategory.Items[i].Value);
    //                DataTable dtSame = BO_HelpCategories.SelectSameParent(upCate, currentCate);
    //                if (dtSame.Rows.Count > 0)
    //                {
    //                    flag = true;
    //                    break;
    //                }
    //            }
    //            if (!flag)
    //            {
    //                upCate = Convert.ToInt32(lbCategory.Items[lbCategory.SelectedIndex - 1].Value);
    //                BO_HelpCategories.UpdateParent(upCate, currentCate);
    //                getCategories();
    //                return;
    //            }
    //        }
    //        else
    //        {
    //            message.InnerHtml = O2S_Message.Warning("Thông Báo", "Danh mục này đã ở vị trí trên cùng !");
    //            return;
    //        }
    //        BO_HelpCategories.UpDown(upCate, currentCate, 1);
    //        //DataRow currRow = BO_HelpCategories.Search(currentCate).Rows[0];
    //        //DataRow upRow = BO_HelpCategories.Search(upCate).Rows[0];
    //        //BO_HelpCategories.UpDown(upCate,
    //        //    Convert.ToInt32(currRow["SORT_DISPLAY"]), 1);
    //        //BO_HelpCategories.UpDown(currentCate,
    //        //    Convert.ToInt32(upRow["SORT_DISPLAY"]), 0);
    //        getCategories();
    //        lbCategory.SelectedValue = currentCate.ToString();
    //    }
    //    catch (Exception ex)
    //    {
    //        message.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi: <br />" + ex.Message);
    //    }
    //}
    //protected void btnDown_Click(object sender, EventArgs e)
    //{
    //    try
    //    {
    //        int downCate = -1;
    //        int currentCate = Convert.ToInt32(lbCategory.SelectedValue);
    //        if (lbCategory.SelectedIndex < lbCategory.Items.Count - 1)
    //        {
    //            bool flag = false;
    //            for (int i = lbCategory.SelectedIndex + 1; i < lbCategory.Items.Count; i++)
    //            {
    //                downCate = Convert.ToInt32(lbCategory.Items[i].Value);
    //                DataTable dtSame = BO_HelpCategories.SelectSameParent(downCate, currentCate);
    //                if (dtSame.Rows.Count > 0)
    //                {
    //                    flag = true;
    //                    break;
    //                }
    //            }
    //            if (!flag)
    //            {
    //                space = 0;
    //                getChild(Convert.ToInt32(lbCategory.SelectedValue));
    //                if (lbCategory.SelectedIndex + space + 1 >= lbCategory.Items.Count)
    //                {
    //                    message.InnerHtml = O2S_Message.Warning("Thông Báo", "Danh mục này đã ở vị trí cuối cùng!");
    //                    return;
    //                }
    //                else
    //                {
    //                    downCate = Convert.ToInt32(lbCategory.Items[lbCategory.SelectedIndex + space + 1].Value);
    //                    BO_HelpCategories.UpdateParent(downCate, currentCate);
    //                    getCategories();
    //                    return;
    //                }
    //            }
    //        }
    //        else
    //        {
    //            message.InnerHtml = O2S_Message.Warning("Thông Báo", "Danh mục này đã ở vị trí cuối cùng!");
    //            return;
    //        }
    //        BO_HelpCategories.UpDown(downCate, currentCate, 0);
    //        //DataRow currRow = BO_HelpCategories.Search(currentCate).Rows[0];
    //        //DataRow downRow = BO_HelpCategories.Search(downCate).Rows[0];
    //        //BO_HelpCategories.UpDown(currentCate, Convert.ToInt32(downRow["SORT_DISPLAY"]), Convert.ToInt32(downRow["UP"]));
    //        //BO_HelpCategories.UpDown(downCate, Convert.ToInt32(currRow["SORT_DISPLAY"]), Convert.ToInt32(downRow["UP"]));
    //        getCategories();
    //        lbCategory.SelectedValue = currentCate.ToString();
    //    }
    //    catch (Exception ex)
    //    {
    //        message.InnerHtml = O2S_Message.Warning("Thông Báo", "Danh mục này đã ở vị trí cuối cùng!");
    //    }
    //}
    protected void getCategories()
    {
        try
        {
            base.OpenConnection();
            lbCategory.Items.Clear();
            lbCategory.Items.Add(new ListItem("Danh mục cao nhất", "0"));
            DataTable objDT = BO_HelpCategories.SearchByParent(0);
            if (objDT.Rows.Count > 0)
            {
                for (int i = 0; i < objDT.Rows.Count; i++)
                {
                    string ident = "---";
                    lbCategory.Items.Add(new ListItem(ident + objDT.Rows[i]["CATEGORY_NAME"].ToString(), objDT.Rows[i]["CATEGORY_ID"].ToString()));
                    getChild_Categories(Convert.ToInt32(objDT.Rows[i]["CATEGORY_ID"].ToString()), ident);
                }
                lbCategory.SelectedIndex = 0;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally { base.CloseConnection(); }
    }
    protected void getChild_Categories(int _parent_id, string ident)
    {
        try
        {
            DataTable objDT = BO_HelpCategories.SearchByParent(_parent_id);
            if (objDT.Rows.Count > 0)
            {
                ident += "--- ";
                for (int i = 0; i < objDT.Rows.Count; i++)
                {
                    lbCategory.Items.Add(new ListItem(ident + objDT.Rows[i]["CATEGORY_NAME"].ToString(), objDT.Rows[i]["CATEGORY_ID"].ToString()));
                    getChild_Categories(Convert.ToInt32(objDT.Rows[i]["CATEGORY_ID"].ToString()), ident);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}
